//
//  LBSuperMarketCategories.m
//  LvoeBeen_Imitation
//
//  Created by Mac on 17/2/19.
//  Copyright © 2017年 itcast. All rights reserved.
//

#import "LBSuperMarketCategories.h"
#import "LBSuperMarketGoodModel.h"

@implementation LBSuperMarketCategories

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

+ (void)loadSuperMarketGoodsWithBlock:(void (^)(NSArray *, NSError *))goodsData {
    [LBDataFactory loadLightSuperMarketWithBlock:^(id data,NSError *error) {
        if (error) {
            goodsData(nil, error);
            return ;
        }
        // 获取分类数据
        NSArray *arrayCategories = data[@"data"][@"categories"];
        NSMutableArray *categories = [NSMutableArray array];
        for (NSDictionary *dict in arrayCategories) {
            LBSuperMarketCategories *category = [LBSuperMarketCategories new];
            [category setValuesForKeysWithDictionary:dict];
            for (NSString *key in dict) {
                if ([key isEqualToString:@"id"]) {
                    [category setValue:dict[key] forKey:@"gid"];
                }
            }
            [categories addObject:category];
        }
        // 根据分类 id ，获取商品清单
        for (LBSuperMarketCategories *category in categories) {
            
            NSArray *arrayGoods = data[@"data"][@"products"][category.gid];
            NSMutableArray *goods = [NSMutableArray array];
            for (NSDictionary *dict in arrayGoods) {
                LBSuperMarketGoodModel *good = [LBSuperMarketGoodModel new];
                [good setValuesForKeysWithDictionary:dict];
                [goods addObject:good];
            }
            category.goods = goods;
        }
        
        goodsData (categories, nil);
    }];
}

@end
